import Vue from 'vue';
import $formula from '@/common/formula.js';
const crypto = require("crypto");
export default {
	namespaced: true,

	state: {
		player: {}
	},

	mutations: {
		commit_player_data(state, payload) {

			state.player = payload;

			if(!state.player.resource.diamond_stone){
				Vue.set(state.player.resource,'diamond_stone',0)
			}
			
			if(!state.player.resource.strengthen_stone){
				Vue.set(state.player.resource,'strengthen_stone',0)
			}
			
			if(!state.player.strengthen){
				Vue.set(state.player,'strengthen',[0,0,0,0,0,0,0,0])
			}
			
			if(!state.player.current_map){
				Vue.set(state.player,'current_map','银杏山谷')
			}
			
			if(!state.player.backpack_count){
				Vue.set(state.player,'backpack_count',105)
			}
			
			
			
			updatePlayerAttributes(state)
		},
		
		update_retrieve_value(state,payload){
			state.player.retrieveValue[payload.type] = payload.value;
		},
		
		update_current_map_value(state,payload){
			state.player.current_map = payload;
		},
		
		update_player_resource(state,payload){

			state.player.resource[payload.type] += payload.value;
			
			//如果是经验
			if(payload.type == 'exp'){
				
				if(state.player.resource.exp >= $formula.player_level_exp(state.player.lv)){
					state.player.resource.exp -= $formula.player_level_exp(state.player.lv)
					state.player.lv += 1
					updatePlayerAttributes(state)
				}
				
			}
			
		},

		commit_put_on_equip(state, equipDataVolume) {
			let bodyEquipDataVolume = state.player.equip.find(item => item.region == equipDataVolume.region);
			let backPackEquipDataVolume = equipDataVolume;

			if (bodyEquipDataVolume) { // 当前要穿戴的装备类型身上已经有了
				state.player.equip.splice(state.player.equip.findIndex(equip => equip.region == equipDataVolume.region),1);

				// 获取替换装备在背包中的索引
				let backpackIndex = state.player.backpack.findIndex(equip => equip.uid == backPackEquipDataVolume.uid);
				state.player.backpack.splice(backpackIndex, 1);

				// 在同样的位置添加新的装备
				state.player.backpack.splice(backpackIndex, 0, bodyEquipDataVolume);

				state.player.equip.push(backPackEquipDataVolume);
				
			} else { // 当前要穿戴的装备类型身上还没有
				state.player.backpack.splice(state.player.backpack.findIndex(equip => equip.uid == equipDataVolume.uid),
					1);
				state.player.equip.push(backPackEquipDataVolume);
			}
			
			
			updatePlayerAttributes(state)

			uni.$emit('equip_pop', '');
			
			//$common.save_game()
			
		},

		commit_take_off_equip(state, equipDataVolume) {
			
			if(state.player.backpack.length >= state.player.backpack_count){
				uni.$emit('game-info',{
					content : `<span style="color:var(--color-yellow)">[警告]</span> <span>背包已满，请清理背包</span>`
				})
				return;
			}
			
			
			// 找到要卸下的装备
			let index = state.player.equip.findIndex(equip => equip.uid == equipDataVolume.uid);
			let bodyEquipDataVolume = state.player.equip.splice(index, 1)[0];
			
			

			// 将装备添加到背包
			state.player.backpack.push(bodyEquipDataVolume);

			uni.$emit('equip_pop', '');
			
			updatePlayerAttributes(state)
			
			//$common.save_game()
		},
		
		//背包增加物品
		commit_backpack_add(state,equipDataVolume){
			
			if(state.player.backpack.length >= state.player.backpack_count){
				uni.$emit('game-info',{
					content : `<span style="color:var(--color-yellow)">[警告]</span> <span>背包已满，请清理背包</span>`
				})
				return;
			}
			
			state.player.backpack.push(equipDataVolume);
		},
		
		//背包删除物品
		commit_backpack_remove(state,equipDataVolume){
			state.player.backpack.splice(state.player.backpack.findIndex(equip => equip.uid == equipDataVolume.uid),1);
		},
		
		//回收物品
		
		
		commit_retrieve_equip(state,equipDataVolume){
			
			if(equipDataVolume.lock) return;
			
			//获得金币
			let equip_price = equipDataVolume.lv * 10 * equipDataVolume.qlty;
			let coin_percent = Math.round(equip_price * state.player.attr.coin_percent/100)
			equip_price += coin_percent;
			
			state.player.resource['coin'] += equip_price;

			
			//品质2,3,4获得强化石
			let strengthen_stone = 0;
			if(equipDataVolume.qlty >= 2){
				strengthen_stone = equipDataVolume.qlty - 1;
				state.player.resource['strengthen_stone'] += strengthen_stone;
			}

			
			uni.$emit('game-info',{
				content : `<span style="color:var(--color-blue)">[回收]</span> 回收装备 <span style='color:var(--qlty-${equipDataVolume.qlty})'>${equipDataVolume.name}</span> 获得 <span style='color:var(--color-yellow)'>${equip_price}</span><span style="font-size:12px;">${coin_percent > 0 ? `(+${coin_percent})` : ''}</span> 金币 ${strengthen_stone > 0 ? `<span style='color:var(--color-yellow)'>${strengthen_stone}</span> 强化石`: ''}`
			});
			
			state.player.backpack.splice(state.player.backpack.findIndex(equip => equip.uid == equipDataVolume.uid),1);
			
		},
		
		//装备上锁
		commit_equip_lock(state,equipDataVolume){
			let lock_equip = state.player.backpack.find(item => item.uid == equipDataVolume.uid);
			if (lock_equip) {
				Vue.set(lock_equip, 'lock', equipDataVolume.lock ? false : true);
			}
		},
		
		//部位强化
		commit_strengthen(state,region){
			
			if(state.player.resource.coin < $formula.need_strengthen_resource(state.player.strengthen[region]).coin || state.player.resource.strengthen_stone < $formula.need_strengthen_resource(state.player.strengthen[region]).stone){
				
				uni.$emit('quick_message','资源不足，请努力攒材料吧！')

				return;
			}
			
			
			state.player.resource['coin'] -= $formula.need_strengthen_resource(state.player.strengthen[region]).coin;
			state.player.resource['strengthen_stone'] -= $formula.need_strengthen_resource(state.player.strengthen[region]).stone;
			

			if(Math.random() * 100 <= $formula.success_rate(state.player.strengthen[region])){
				
				Vue.set(state.player.strengthen, region, state.player.strengthen[region] + 1);

			}else{
				
				if(state.player.strengthen[region] > 20){
					Vue.set(state.player.strengthen, region, state.player.strengthen[region] - 1);
					
					uni.$emit('quick_message','强化失败，强化等级-1，请再接再厉！')
					
				}else{
					uni.$emit('quick_message','强化失败，请再接再厉！20 级之前不降强化等级！')
				}
				
				
			}
			
			updatePlayerAttributes(state);
			
			if(state.player.strengthen[region] % 10 == 0){
				Vue.prototype.$common.send_message(`<span style="color:var(--color-yellow)">[传闻]</span> <span style="color:var(--color-green)">${state.player.nickname}</span> 将 <span style="color:var(--color-purple)">${getApp().globalData.equip_name[region]}</span> 强化到了 <span style="color:var(--color-red2)">${state.player.strengthen[region]}</span> 级，太让人羡慕了！`)
			}
			
			Vue.prototype.$common.save_game(false)
			
		},


	}
}


function updatePlayerAttributes(state) {
	
	state.player.attr = JSON.parse(JSON.stringify(state.player.base_attr));
	
	//遍历装备属性
	let player_equips = state.player.equip;
	for(let i=0;i<player_equips.length;i++){
		for (let key in player_equips[i]) {
			
			if(state.player.attr.hasOwnProperty(key)){
				state.player.attr[key] += Math.max(0,player_equips[i][key])
			}
			
			// if (key == "resid" || key == "name" || key == "uid" || key == "region" || key == "qlty" || key == "describe" || key == "_id" || key == "lv" || key == "strengthen_count") continue;
			// state.player.attr[key] += Math.max(0,player_equips[i][key])
		}
	}
	
	//根据玩家等级增加属性
	state.player.attr['min_ac'] += parseInt(state.player.lv / 5) 
	state.player.attr['max_ac'] += parseInt(state.player.lv / 5)
	state.player.attr['hp'] += parseInt(state.player.lv*2)
	
	//根据玩家部位强化等级增加属性
	for(let i=0;i<state.player.strengthen.length;i++){
		//0武器[攻击] 1衣服[防御] 2手镯[防御] 3戒指[攻击] 4头盔[防御] 5项链[攻击] 6腰带[血量] 7鞋子[血量]
		if(i == 0 || i == 3 || i == 5){
			state.player.attr['min_ac'] += state.player.strengthen[i]
			state.player.attr['max_ac'] += state.player.strengthen[i]
		}
		if(i == 1 || i == 2 || i == 4){
			state.player.attr['min_def'] += state.player.strengthen[i]
			state.player.attr['max_def'] += state.player.strengthen[i]
		}
		if(i == 6 || i == 7){
			state.player.attr['hp'] += state.player.strengthen[i] * 2
		}
	}
	
	


}
